import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-14
 * Time: 22:45
 */
public class MyStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    //构造方法
    public MyStack() {
    queue1 = new LinkedList<>();
    queue2 = new LinkedList<>();
    }

    //将元素 x 压入栈顶。
    public void push(int x) {
        if (!queue1.isEmpty()) {
            queue1.offer(x);
        }else if (!queue2.isEmpty()) {
            queue2.offer(x);
        }else {
            queue1.offer(x);
        }
    }

    //移除并返回栈顶元素。
    public int pop() {
        if (empty()) {
            return -1;
        }
        if (!queue1.isEmpty()) {
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        }else {
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        }
    }

    //返回栈顶元素。
    public int top() {
        if (empty()) {
            return -1;
        }
        if (!queue1.isEmpty()) {
            int size = queue1.size();
            int val = 0;
            for (int i = 0; i < size; i++) {
                val = queue1.poll();
                queue2.offer(val);
            }
            return val;
        }else {
            int size = queue2.size();
            int val = 0;
            for (int i = 0; i < size; i++) {
                val = queue2.poll();
                queue1.offer(val);
            }
            return val;
        }
    }

    //如果栈是空的，返回 true ；否则，返回 false 。
    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}
